package com.pig4cloud.pig.homecenter.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.pig4cloud.pig.homecenter.enums.TrueFalse;

import java.io.IOException;

public class CustomTrueFalseDeserializer extends JsonDeserializer<TrueFalse> {

	@Override
	public TrueFalse deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
		switch (p.getCurrentToken()){
			case VALUE_FALSE:
				return TrueFalse.FALSE;
			case VALUE_TRUE:
				return TrueFalse.TRUE;
			case VALUE_STRING:
				if("TRUE".equals(p.getText().toUpperCase())){
					return TrueFalse.TRUE;
				}else{
					return TrueFalse.FALSE;
				}
			default:
				return TrueFalse.FALSE;
		}
	}
}